/**
 * Created by PC on 11/17/14.
 */

$(document).ready(function(){
    // render
    loadData();
    // event
    bindEvent();
});

function loadData() {
    ISS.BACKEND.request({
        url: '../../service/admin/ad_GetLawList',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapLawHypothesisToView(responseContent);
        }
    });

    ISS.BACKEND.request({
        url: '../../service/admin/ad_GetLawListReason',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapLawReasonToView(responseContent);
        }
    });
}

function bindEvent() {
    ISS.EVENT.bindEvent('#RegeditFormLaw_submit', 'click', function () {
        if(validate()){
            sendInfo();
        }
        else {
            var htmlText = '<div class="box s-error mbs msgBox l-pageWrapper mbm">\
                                <div class="pam">Đăng kí luật mới thất bại</div>\
                            </div>';
            ISS.DOM.before('.l-register__benefits', htmlText);
        }
    });

    ISS.EVENT.bindEvent('#AdLawCommon_Delete', 'click', function () {
        deleteLaw();
    });

    ISS.EVENT.bindEvent('#AdLawCommon_Modified', 'click', function () {
        addLawNew();
    });
}

function addLawNew() {
    var _vt = '', _vp = '';
    ISS.DOM.forEach('#LawTable input[type="checkbox"]:checked', function(){
        if(!_vt) {
            _vt = $(this).val();
        }
        else {
            _vt += '^' + $(this).val();
        }
    });

    ISS.DOM.forEach('#LawTable_Result input[type="checkbox"]:checked', function(){
        if(!_vp) {
            _vp = $(this).val();
        }
        else {
            _vp += '^' + $(this).val();
        }
    });

    if(!_vt) {
        alert('Chưa chọn giả thiết');
    }
    else if(!_vp) {
        alert('Chưa chọn kết luận');
    }
    else if(_vt && _vp) {
        var luat = {};
        luat.veTrai = _vt;
        luat.vePhai = _vp;
        ISS.BACKEND.request({
            url: '../../service/admin/header/registerLawAddNew',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            type: 'POST',
            dataType: 'json',
            data: JSON.stringify(luat),
            success: function (data) {
                if (data.code == 0) {
                    alert('Thêm thành công');
                    loadData();
                }
                else {
                    alert('Thêm không thành công');
                }
            }
        });
    }

}

function deleteLaw() {
    var _laws = [];
    var _colors = [];
    ISS.DOM.forEach('#form-AdLawCommon input[type="checkbox"]:checked', function(){
        var law = {};
        law.maLuat = $(this).val();
        _laws.push(law);
    });

    if(_laws.length > 0) {
        ISS.BACKEND.request({
            url: '../../service/admin/header/registerLawDelete',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            type: 'POST',
            dataType: 'json',
            data: JSON.stringify(_laws),
            success: function (data) {
                var responseContent = data.responseObject;
                if(data.code == 0) {
                    alert('Xoá thành công');
                    loadData();
                }
                else {
                    alert('Xoá không thành công');
                }
            }
        });
    }
    else {
        alert('Chưa chọn dòng nào');
    }
}

function sendInfo() {
    var law = {};

    law.maLuat = ISS.DOM.getVal('#RegeditFormLaw_code');
    law.tenLuat = ISS.DOM.getVal('#RegeditFormLaw_name');
    law.gTKL = ISS.DOM.getVal('#RegeditFormLaw_selection');

    ISS.BACKEND.request({
        url: '../../service/admin/header/registerLaw',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(law),
        success: function (data) {
            var responseContent = data.responseObject;
            if(data.code == 0) {
                alert('Thêm thành công');
                ISS.WINDOW.location('/jsp/admin/ad-law-common.jsp');
            }
            else {
                alert('Thêm không thành công');
            }
        }
    });
}

function clearValidate() {
    ISS.DOM.removeClass('#RegistrationForm_email', 'error');
    ISS.DOM.removeClass('#RegistrationForm_password', 'error');
}

function validate() {
    var result = true;
    return result;
}

function mapLawHypothesisToView(hypothesis) {

        var rows = hypothesis.length;
        var columns = 1;
        var str = '';
        str += '<table id="LawTable" class="tableLawlWidth align-center">';
        str += '<thead>';
        str += '   <tr>';
        str += '       <th class="columnLawSTT">STT</th>';
        str += '       <th class="columnLawStick">Tick</th>';
        str += '       <th class="columnLawName">Tên giả thuyết</th>';
        str += '   </tr>';
        str += '</thead>';
        str += '<tbody>';
        for(var i = 0; i < rows; i++){
            if (i%2==0){
                str += '<tr onmouseover="set_hover(this);" onmouseout="remove_hover(this)" id="showData" class="column-Light tableTransparent">';
            }else{
                str += '<tr onmouseover="set_hover(this);" onmouseout="remove_hover(this)" id="showData" class="column-Light tableTransparent">';
            }

            for (var j = 0; j < columns; j++){
                str += '<td class="columnLawSTT tableLawPadding">' + (i+1) + '</td>';
                str += '<td class="columnLawStick tableLawPadding">';
                str += '<input onclick="highlight_row(this)" id="' + hypothesis[i].maLuat + '" type="checkbox" value="' + hypothesis[i].maLuat + '"/></td>';
                str += '<td class="columnLawName tableLawPadding">' + hypothesis[i].tenLuat + '</td>';
            }
            str += '</tr>';
        }
        str +=   '</tbody>';
        str += '</table>';
    ISS.DOM.html('#Law_hypothesis', str);

}

function mapLawReasonToView(hypothesis) {

    var rows = hypothesis.length;
    var columns = 1;
    var str = '';
    str += '<table id="LawTable_Result" class="tableLawlWidth align-center">';
    str += '<thead>';
    str += '   <tr>';
    str += '       <th class="columnLawSTT">STT</th>';
    str += '       <th class="columnLawStick">Tick</th>';
    str += '       <th class="columnLawName">Tên kết luận</th>';
    str += '   </tr>';
    str += '</thead>';
    str += '<tbody>';
    for(var i = 0; i < rows; i++){
        if (i%2==0){
            str += '<tr onmouseover="set_hover(this);" onmouseout="remove_hover(this)" id="showData" class="column-Light tableTransparent">';
        }else{
            str += '<tr onmouseover="set_hover(this);" onmouseout="remove_hover(this)" id="showData" class="column-Light tableTransparent">';
        }

        for (var j = 0; j < columns; j++){
            str += '<td class="columnLawSTT tableLawPadding">' + (i+1) + '</td>';
            str += '<td class="columnLawStick tableLawPadding">';
            str += '<input onclick="highlight_row(this)" id="' + hypothesis[i].maLuat + '" type="checkbox" value="' + hypothesis[i].maLuat + '"/></td>';
            str += '<td class="columnLawName tableLawPadding">' + hypothesis[i].tenLuat + '</td>';
        }
        str += '</tr>';
    }
    str +=   '</tbody>';
    str += '</table>';
    ISS.DOM.html('#Law_reasson', str);

}